package com.example.my.agiftthat.ui.list.intentlist.fragmentsuk;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.my.agiftthat.R;
import com.example.my.agiftthat.config.Url;
import com.example.my.agiftthat.helper.GlideImageLoader;
import com.example.my.agiftthat.helper.OkHttpClientHelper;
import com.example.my.agiftthat.ui.list.FragmentAdapter;
import com.example.my.agiftthat.ui.list.UserBean;
import com.google.gson.Gson;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;

import org.w3c.dom.Text;

import java.io.IOException;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class FragmentSUK extends Fragment {
    private String path;
    private String path1;
    private TextView text4Single;
    @Bind(R.id.recyclerView_single)
    RecyclerView recyclerViewSingle;
    @Bind(R.id.banner_single)
    Banner bannerSingle;
    @Bind(R.id.text1_single)
    TextView text1Single;
    @Bind(R.id.text2_single)
    TextView text2Single;
    @Bind(R.id.text3_single)
    TextView text3Single;
    @Bind(R.id.text5_single)
    TextView text5Single;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    SUKBean bean = (SUKBean) msg.obj;
                    SUKBean.DataBean data = bean.getData();
                    bannerSingle.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
                    bannerSingle.setImageLoader(new GlideImageLoader());
                    Log.i("aaaaa",data.getImage_urls()+"");
                    //设置图片集合
                    bannerSingle.setImages(data.getImage_urls());
                    //设置自动轮播 默认为true
                    bannerSingle.isAutoPlay(false);
                    //设置轮播时间
//                    bannerSingle.setDelayTime(2000);
                    //设置指示器位置（当banner模式中有指示器时）
                    bannerSingle.setIndicatorGravity(BannerConfig.CENTER);
                    //banner设置方法全部调用完毕时最后调用
                    bannerSingle.start();
                    text1Single.setText(data.getShort_description());
                    text2Single.setText(data.getName());
                    text3Single.setText("￥ " + data.getPrice());
                    text4Single.setText(data.getLikes_count() +"");
                    text5Single.setText(data.getDescription());
                    break;
                case 1:
                    SUKBeans bean1 = (SUKBeans) msg.obj;
                    List<SUKBeans.DataBean.RecommendItemsBean> list = bean1.getData().getRecommend_items();
                    FragmentSUKAdapter adapter = new FragmentSUKAdapter(getActivity(),list);
                    recyclerViewSingle.setAdapter(adapter);

                    break;
            }
        }
    };



    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_fragment_suk, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        text4Single = (TextView) view.findViewById(R.id.text4_single);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        recyclerViewSingle.setHasFixedSize(true);
        GridLayoutManager manager = new GridLayoutManager(getActivity(),2);
        recyclerViewSingle.setLayoutManager(manager);
        bundle();
        initData(path);
        initDatas(path1);


    }

    private void bundle() {
        Bundle bundle = getArguments();
        int index = bundle.getInt("index");
        //详细描述的网址
        path = Url.LIST_DJ + index;
        //网格布局的网址
        path1 = Url.LIST_DJ + index + Url.LIST_DJSS;
    }

    private void initDatas(final String paths) {
        new Thread(new Runnable() {
            @Override
            public void run() {

                try {
                    String run = OkHttpClientHelper.getStringFromURL(getActivity(), paths, 2);
                    Gson gson = new Gson();
                    SUKBeans bean = gson.fromJson(run, SUKBeans.class);
                    Message msg = handler.obtainMessage();
                    msg.what = 1;
                    msg.obj = bean;
                    handler.sendMessage(msg);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private void initData(final String paths) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String run = OkHttpClientHelper.getStringFromURL(getActivity(), paths, 1);
                    Gson gson = new Gson();
                    SUKBean bean = gson.fromJson(run, SUKBean.class);
                    Message msg = handler.obtainMessage();
                    msg.what = 0;
                    msg.obj = bean;
                    handler.sendMessage(msg);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
